package com.future.baselib.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;


public class ToastUtil {

    private static ToastUtil instance;
    private String text;
    private Context context;
    private Toast toast;
    private long time;

    public synchronized static ToastUtil getInstance() {
        if (null == instance) {
            instance = new ToastUtil();
        }
        return instance;
    }

    public void show(Context context, String text) {
        show(context, text, 0);
    }

    public void show(Context context, String text, int type) {
        if (context == null || TextUtils.isEmpty(text))
            return;

        long  time = System.currentTimeMillis();
        if (this.context == context && this.text == text&&(time-this.time<1500))
            return;
        this.time =time;

        if (toast == null) {
            toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
        }

        toast.setText(text);
        if (type == 1) {
            toast.setDuration(Toast.LENGTH_LONG);
        } else {
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        this.context = context;
        this.text = text;
        toast.show();
    }
}
